// pages/sign/sign.js
const lateListItem = require('../../bean/latelistitem.js')
const abnormalListItem = require('../../bean/abnormallistitem.js')
const utils = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading:true,
    latelist:[],
    abnormalList: [],
    isVisibleAbnormalList:false,
    isVisibleLateList:false,
    multiArray: [['出勤', '缺勤', '请假', '迟到', '早退'], ['事假', '病假']],
    multiIndex: [2, 1],
    clickItemIndex:0,
    countdownText: '00:10:00s',
    countdownSecond: 10 * 60 * 1000, // 默认倒计时10min
  },

/**
 * 初始化获取列表数据
 */
  getData:function () {
    var that = this
    var lateList = []
    var abnormalList = []
    
    // 模拟缺勤学生数据
    for (var i = 0; i < 35; i++) {
      var l = lateListItem.getInstance(i, '等待', '张小明', '201805020011', '缺勤')
      lateList.push(l);  
    }

    // 模拟异常学生数据
    for (var i = 0; i < 2; i++) {
      var l = abnormalListItem.getInstance(i, '张小明', '201805020011', '距离1.3km')
      abnormalList.push(l);  
    }

    that.setData({
        latelist: lateList,
        abnormalList: abnormalList
    })

    // 数据加载完成 -- 取消等待加载界面
    that.setData({
      showLoading: false
    })
  },

  // 打印学生列表数据到终端
  showData:function () {
    var that = this
    var list = that.data.latelist
    for (var i = 0; i < list.length; i++) {
      console.log(list[i])
    }
  },

  // 开启倒计时 -- 默认倒计时10min
  startCountdown: function () {
    var that = this
    var countdownSecond = that.data.countdownSecond

    // 递归调用实现倒计时的约束条件
    if (countdownSecond <= 0) {
      that.setData({
        countdownText: '倒计时结束'
      })
      // timeout跳出递归
      return 
    }

    that.setData({
      countdownText: utils.countDownDateFormat(countdownSecond)
    })
    
    setTimeout(function () {
      // 放在最后--
      that.setData({
        countdownSecond: that.data.countdownSecond - 1000
      })
      that.startCountdown()
    }, 1000)
  },

  /**
   * ------------- 点击事件 ------------
   */
  // 立即停止倒计时
  stopCountdownClick: function (e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '您确定要结束倒计时？',
      showCancel: true,
      cancelText: '取消',
      // cancelColor: '#f00',
      confirmText: '立即结束',
      // confirmColor: '',
      success: function(res) {
        if (res.confirm) {
          console.log('收到！小的马上风风火火结束掉这该死的倒计时！！！')
          that.setData({
            countdownSecond:0
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 跳转到缺勤记录页面
  turnToAbsenceClick: function () {
    var that = this
    wx.navigateTo({
      url: '/pages/absence_record/absenceRecord',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 显示隐藏异常人数
  abnormallistvisibleclick:function (e) {
    var that = this
    that.setData({
      isVisibleAbnormalList: !that.data.isVisibleAbnormalList
    })
  },
  // 显示隐藏缺勤人数
  latelistvisibleclick:function (e) {
    var that = this
    that.setData({
      isVisibleLateList: !that.data.isVisibleLateList
    })
  },
  // 缺勤人数item点击事件
  lateitemclick:function (e) {
    var that = this
    console.log('-----缺勤人数 : ')
    console.log(e)
    that.setData({
      clickItemIndex: e.currentTarget.dataset.index
    })
  },
  //多列选择器
  bindMultiPickerChange: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
    var l = that.data.latelist
    var i = that.data.clickItemIndex
    var a = that.data.multiArray
    var multiIndex = that.data.multiIndex
    l[i].reason = a[0][multiIndex[0]]
    that.setData({
      latelist: l
    })
  },
  //多列选择器，某一列的值改变时触发事件
  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 初始化并且获取学生数据
    that.getData()
    // that.showData();
    // 开启倒计时
    that.startCountdown();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})